package utils

import (
	"os/exec"
	"runtime"
	"strings"
)

const (
	Windows      = "windows"
	Linux_a7     = "linux_a7"
	Linux_nano   = "linux_nano"
	Linux_nano46 = "linux_nano_46"
	Linux_rk3568 = "linux_rk3568" //4.19.193
	// Linux_nx       = "linux_nx"
	Linux_3352     = "linux_3352" //3.2.0
	Linux_desk     = "Linux_desk"
	Linux_jetson_5 = "linux_jetson_5"
)

var platform string

// 初始化调用
func platformInit() {
	platform = GetPlatform()
}

/**************************************************************************
* 函数名称: BashOutput
* 功能描述: bash脚本输出得到信息
* 输入参数: write 要输出脚本的写入值
* 返 回 值: string 返回值
* 其它说明: 无
**************************************************************************/
func BashOutput(write string) (string, error) {
	var read string
	cmd := exec.Command("/bin/sh", "-c", write)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return read, err
	}
	read = string(out)
	return read, err
}

/**************************************************************************
* 函数名称: GetPlatform
* 功能描述: 获取当前所在的平台
* 输入参数: 无
* 返 回 值: string 所在平台名称
* 其它说明: 无
**************************************************************************/
func GetPlatform() string {
	var platform string
	switch runtime.GOOS {
	case "windows":
		platform = Windows
	case "linux":
		output, _ := BashOutput("uname -r")
		if strings.Contains(output, "4.9.253-tegra") {
			platform = Linux_nano46
		} else if strings.Contains(output, "4.9.140") {
			platform = Linux_nano
		} else if strings.Contains(output, "4.19.193") {
			platform = Linux_rk3568
		} else if strings.Contains(output, "3.2.0") {
			platform = Linux_3352
		} else if strings.Contains(output, "4.1.15-myimx6") {
			platform = Linux_a7
		} else if strings.Contains(output, "5.10.104-tegra") {
			platform = Linux_jetson_5
		} else {
			platform = Linux_desk
		}
	}
	return platform
}

// 直接取值,不会每次都调用系统参数
func GetPlatformValue() string {
	return platform
}
